﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using Mimas.Framework.Shared.Data.EnumObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class PersonAdressMapping
    {
        public static PersonAdress Map(PersonAdressDTO data)
        {
            PersonAdress newItem = new PersonAdress();

            newItem.CountryId = data.CountryId;
            newItem.Id = data.Id;
            newItem.PersonId = data.PersonId;
            newItem.Text = data.Text;
            newItem.TownId = data.TownId;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static PersonAdressDTO Map(PersonAdress data)
        {
            PersonAdressDTO newItem = new PersonAdressDTO();

            newItem.CountryId = data.CountryId;
            newItem.Id = data.Id;
            newItem.PersonId = data.PersonId;
            newItem.Text = data.Text;
            newItem.TownId = data.TownId;

            return newItem;
        }

        public static PersonAdress[] Map(PersonAdressDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static PersonAdressDTO[] Map(PersonAdress[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
